package logic

import (
	"context"
	"fmt"
	"home-view/models/mysql"
	"math/rand"
	"time"

	"home-view/homerpc/homerpc"
	"home-view/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *homerpc.RegisterRequest) (*homerpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{
		Model:    gorm.Model{},
		Phone:    in.Phone,
		Password: in.Password,
		Image:    "https://ts1.tc.mm.bing.net/th/id/OIP-C.bxjli_O6y87iJst238CJowHaD-?w=265&h=200&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
		NickName: fmt.Sprintf("fy%s%d", time.Now(), rand.Intn(10000)),
	}

	if err := user.UserAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &homerpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
